<?php
/**
 * Created by PhpStorm.
 * User: david
 * Date: 2017/8/22
 * Time: 19:10
 * 开桌王，上桌王脚本
 */
class ActivityDataHandler  extends ScriptBase{
    
    private $logs_db;
    private $game_db;
    
    public function logic()
    {
        return 0;
        set_time_limit(0);
        $this->logs_db = $this->mysql('logs');
        $this->game_db = $this->mysql('game');
        
        //开桌王
        $date = date("Y-m-d",strtotime("-1 day"));        
        $mysql_es = $this->mysql('esmj_config');
        $mysql_logs = $this->mysql('daq');
        set_time_limit(0);
        $sql_logs = "call sp_get_desk_top('{$date}',0,300);";
        $sql_header = "INSERT INTO t_desk_top(create_date,rank,uid,nickname,open_desk_num,dismiss_desk_num,real_open_desk_num) VALUES";
        $result=$mysql_logs->query($sql_logs);
        $value='';
        if($result) {
            $result_data = $mysql_logs->fetch_all();
            foreach ($result_data as $k=>$val){
                if($value){
                   $value=sprintf("('%s',%u,%u,'%s',%u,%u,%u),",$date,$val['rank'],$val['i_uid'],$val['nickname'],$val['open_desk_num'],$val['dismiss_desk_num'],$val['real_open_desk_num']).$value;
                }else{
                   $value=sprintf("('%s',%u,%u,'%s',%u,%u,%u)",$date,$val['rank'],$val['i_uid'],$val['nickname'],$val['open_desk_num'],$val['dismiss_desk_num'],$val['real_open_desk_num']);
                }
            }
            if ($value){
                $sql_insert=$sql_header.$value;
                $mysql_es->query($sql_insert);
            }
        }
        //上桌王
        //$mysql_esmj = $this->mysql('esmj'); 
        //$sql_logs = "call sp_get_plays_top('{$date}');";
        //$result=$mysql_esmj->query($sql_logs);
        
        $p = 0;
        while (true){
            $rs = $this->plays_top($date,$p);
            if(!$rs) break;
            $p++;
            usleep(100000);
        }
        
        $sql = "UPDATE t_plays_top m ,
        (SELECT  @rownum := @rownum + 1 AS rownum,obj.uid,obj.real_play_num
        FROM (SELECT real_play_num,uid
        FROM t_plays_top
        where create_date = '{$date}'
        ORDER BY real_play_num DESC,uid ASC) AS obj,
        (SELECT @rownum := 0) r ) n
        SET m.rank = n.rownum WHERE m.uid = n.uid";
        $this->game_db->query($sql);
    }
    
    
    private function plays_top($date,$p=0){
        $listrow = $p*1000;
        $sql = "SELECT uids,seq FROM log_paiju WHERE 
        time>= '{$date} 00:00:00' AND time<= '{$date} 23:59:59' AND seq = juShu 
        limit {$listrow},1000";
        
        $this->logs_db->query($sql);
        $list = $this->logs_db->fetch_all();
        if(empty($list)) return false;    
        foreach ($list as $val){
            $uids = json_decode($val['uids'],true);
            $jushu = $val['seq'];
            foreach ($uids as $uid){
                $sql = "insert into t_plays_top(create_date,uid,nickname,real_play_num) 
           values('{$date}','{$uid}',ifnull((select nickname from esmj.d_user a where a.uid = {$uid}),''),'{$jushu}') 
               ON DUPLICATE KEY UPDATE real_play_num = real_play_num + '{$jushu}'";
                $this->game_db->query($sql);
            }
        }
        return true;
    }
    
}